var cci = {};

var rootId;
var selectedTreeLabel = undefined;
var selectedInputId = undefined;
var isTreeDragEnd = false;

cci.make = function(rootId) {
	var json = ccidata.getJson();
	var groupBoxId = cci.makeRoot(rootId, json.GroupType, "group");
	cci.generateWithJson(groupBoxId, json);
};

cci.generateWithJson = function(parentId, valueData) {
	if(valueData.values) {
		for(var i=0; i<valueData.values.length; i++) {
			cci.addEntity(parentId.split("_")[1], valueData.values[i]);
		}
	}

	if(valueData.groups) {
		for(var i=0; i<valueData.groups.length; i++) {
			if(valueData.groups[i] == null) continue;
			var groupBoxDiv = cci.makeGroup(parentId, valueData.groups[i].GroupType);
			$("#parent_"+parentId.split("_")[1]).append(cci.createDiv("","spacer")).append(groupBoxDiv);
			$('#'+groupBoxDiv.attr("id")).panel({ collapsible:true });
			
			var boxId = groupBoxDiv.attr("id");
			if(valueData.groups[i].values) cci.generateWithJson(boxId, valueData.groups[i]);
		}
	}
};

cci.addGroup = function(rootDivId, groupTypeValue, groupType) {
	var idNumber2 = $("#idNumber2").val();
	if(idNumber2 > 0 && groupType == "group") {
		alert("There can be only one group.");
		return;
	}

	if(groupType === "group") rootId = rootDivId;
	cci.makeRoot(rootDivId, groupTypeValue, groupType);
	$("#idNumber2").val(parseInt(parseInt(idNumber2,10) + parseInt(1,10)));
};

cci.makeRoot = function(rootDivId, groupTypeValue, groupType) {
	var groupBoxDiv = cci.makeGroup(rootDivId, groupTypeValue);
	jQuery("#"+rootDivId).append(groupBoxDiv);
	
	$('#'+groupBoxDiv.attr("id")).panel({
        collapsible:true
    });	
	return groupBoxDiv.attr("id");
};

cci.getSelectBoxValue = function(selectBoxId) {
	var selectedValue = $("#"+selectBoxId+ " option:selected").val();
	return selectedValue;
};

cci.addEntity = function(idNumber, valueData) {
	var groupMainElementDiv = cci.makeGroupMainElementDiv(idNumber, valueData);
	$("#parent_"+idNumber).append(groupMainElementDiv);
};

cci.removeEntity = function(removeEntityId) {
	$("#"+removeEntityId).remove();
};

cci.removeGroup = function(targetDiv) {
	$("#"+targetDiv).remove();
};

cci.makeGroup = function(parentId, cciValue) {
	var idNumber = $("#idNumber").val();
	var subEntityDiv = cci.createDiv("parent_" + idNumber, "parent centralPanel");
	$("#idNumber").val(parseInt(parseInt(idNumber,10) + parseInt(1,10)));
	
	return cci.createDiv("group_" + idNumber, "group centralPanel")
			  .append("<h3 id='title_"+idNumber+"' class='title'>"+ cciValue +"</h3>")
			  .append(cci.createDiv("grp_" + idNumber, "main centralPanel").append(subEntityDiv))
              .append("\n");
};

cci.makeGroupMainElementDiv = function(idNumber, valueData) {
	var entityCnt = $("entity").size();
	var entityId = "entity_" + idNumber + "_" + entityCnt;
	
	var removeEntityBtn = cci.makeBtn(" - ", undefined, 'click', function() { cci.removeEntity(entityId); });
	var nameBox = cci.createInputTextBox("entity_name_"+idNumber+"_"+entityCnt, "", valueData.name)
				     .bind("mouseover", function() { cci.setSelectedBoxId(this); });
	var valueBox = cci.createInputTextBox("entity_value_"+idNumber+"_"+entityCnt, "", valueData.value)
					 .bind("mouseover", function() { cci.setSelectedBoxId(this); });
	var hasValueData = ccidata.getHasValueData();
	var typeSelectbox = comm.util.makeCombobox(hasValueData, "entity_type_"+idNumber+"_"+entityCnt, "--", valueData.restriction);
	
	removeEntityBtn.addClass(rootId);
	return cci.createDiv("main_"+idNumber+"_"+entityCnt, "main")
			  .append(cci.createDiv(entityId, "").append("&nbsp;").append(nameBox).append("&nbsp;")
						   .append(typeSelectbox).append("&nbsp;")
						   .append(valueBox).append("&nbsp;")
						   .append(removeEntityBtn).append("&nbsp;")
						   .append($("<entity id='entity'></entity>")));
};

cci.setSelectedBoxId = function(obj) {
	if(selectedTreeLabel) {
		selectedInputId = $("#"+obj.id);
	}
};

cci.createInputTextBox = function(id, classStr, value) {
	var inputBox = $("<input/>").attr("type", "text").attr("id", id).attr("value", value).addClass(classStr);
	return inputBox;
};

cci.makeBtn = function(text, id, eventType, callback) {
	var btn = jQuery("<button></button>");
	if(id) btn.attr("id", id);
	btn.text(text);
	btn.bind(eventType, callback);
	
	return btn;
};

cci.createDiv = function(id, classStr) {
	var div = $("<div></div>").attr("id", id).addClass(classStr);
	return div;
};

cci.makeDynaTree = function(treeDivStr) {
	var treeData = ccidata.getTreeData();
	$("#"+treeDivStr).dynatree({children:treeData,
		dnd: {
				onDragStart:function(node) {
					selectedTreeLabel = node.data.title;
					return true;
				},
				onDragStop:function(node) {
					if(selectedInputId && selectedTreeLabel) {
						selectedInputId.val(selectedTreeLabel);
					}
					selectedTreeLabel = undefined;
					selectedInputId = undefined;
					return false;
				}
			 }
		});
};

cci.makeList = function(listDivStr) {
	var data = ccidata.getListData();
	var cnt = data.length;
	
	var div = $("<div style='padding:2;margin:2;overflow:auto;height:290px'/>");
	for(var i=0; i<cnt; i++) {
		div.append("<div><a href='#;'>"+data[i].CompanyName+"</a></div>");
	}
	
	$("#"+listDivStr).append(div);
};










